package com.platinadesign.axa.view
{
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class Preloader extends Sprite
	{
		private var _body : Sprite;

		public function Preloader( skin : Class )
		{
			_body = ( new skin()) as Sprite;
			this.addChild(body);
			(body['perloaderx'] as TextField).setTextFormat(new TextFormat("ArialBold",16,0x113184,true));
			(body['perloadery'] as TextField).setTextFormat(new TextFormat("ArialBold",16,0x113184,true));
			
		}

		public function get body():Sprite
		{
			return _body;
		}

		public function setProgress( currentIndex : int, totalIndex : int, loadedResourceName : String ) : void
		{	
			if( body[ "pm" ] && body[ "pm" ] is MovieClip )
			{
				trace(currentIndex+" , "+totalIndex);
				if(currentIndex == totalIndex)
					this.remove();
				else{
					body["pm"].width =  Math.floor(912 / totalIndex)*(currentIndex+2) ;
					(body['percentage'] as TextField).setTextFormat(new TextFormat("ArialBold",30,0x113184,true));
					const percentage:Number = Math.floor((currentIndex+2)/totalIndex*100);
					body['percentage'].htmlText = percentage;
				}
			}
		}
		public function remove() : void
		{
			TweenLite.to( this, 2, { alpha: 0, onComplete: completeHandler });
		}

		private function completeHandler() : void
		{
			this.parent.removeChild( this );
		}
	}
}